/**
 * Created by dbwyxl on 2017/9/11.
 */
module.exports =function($cookies,$scope, $rootScope, $state,httpservice, orderReceivingService,paginationService, Common){
  /*获取接单中心 总数/未读数*/
  $scope.getOrderTotal=getOrderTotal;
  /*服务类型*/
  $scope.getServiceType = getServiceType;
  /*业务类型*/
  $scope.getBusinessType = getBusinessType;
  /* 订单状态 */
  $scope.getStatusName =getStatusName;
  /*分页*/
  $scope.pageSetting = paginationService.getDefaultPageSetting();
  /* 获取列表数据 */
  $scope.getConsultOrderPageList = getConsultOrderPageList;
  /*客户详情*/
  $scope.getCustomerDetails = getCustomerDetails;

  //页面控制变量
  //服务类型
  $scope.serviceType = 1;
  //订单状态
  $scope.orderType = '';
  //业务类型
  $scope.businessType = '';
  //控制状态显示
  $scope.showStatus = 1;
  //控制业务类型显示
  $scope.showYw = false;
  //业务类型列表
  $scope.businessTypeList = [];
  //业务类型名称
  $scope.businessName = '全部';


  $scope.init=function(){
    //初始化
    getOrderTotal();
    getConsultOrderPageList();
    getServiceType();
    getBusinessType();
  };
  $scope.init();

  function getOrderTotal(){
    orderReceivingService.getOrderTotal().then(function(data){
      if(data.data.code==1){
        $scope.orderTotal=data.data.data;
      }
    })
  }

  function getServiceType(){
    orderReceivingService.getServiceType().then(function(data){
      if(data.code==1){
        $scope.caseSourceList=data.data
      }
    })
  }


  function getBusinessType(){
    orderReceivingService.getBusinessType().then(function(data){
      if(data.data.code==1){
        $scope.businessListClassify=data.data.data;
        //$scope.businessListClassify=Common.formatArr(data.data.data, 'code');
      }
    })
  }


  function getStatusName(type) {
    if(type == 11){
      return "待解答";
    }else if(type == 12){
      return "已解答";
    }else if(type == 21){
      return "服务中";
    }else if(type == 22){
      return "待验收";
    }else if(type == 3){
      return "已完成";
    }else if(type == 4){
      return "已评价";
    }
  }

  function getConsultOrderPageList() {

    //$scope.focus = index;//证据组 选中样式

    //var businesstypess= [];
    var newCaseSources={
      'currentPage':$scope.pageSetting.pageNo,//当前页
      'pageSize':$scope.pageSetting.pageSize,//分页数
      'serviceType':parseInt($scope.serviceType),//服务类型
      'businessType':parseInt($scope.businessType),//业务类型
      'status':parseInt($scope.orderType)//状态
    };

    // var params=JSON.stringify(newCaseSources);
    orderReceivingService.getConsultOrderPageList(newCaseSources).then(function(data){
      if(data.code==1){
        $scope.consultOrderPageList=data.data.list;
        $scope.pageSetting.totalItems = data.data.totalCount
      }
    })
  }
  //切换服务类型
  $scope.changeServiceType = function ( event ) {
    $scope.serviceType = event.currentTarget.dataset.type;

    $scope.orderType = '';  //重置状态
    if($scope.serviceType == 1) {
      $scope.showStatus = 1;
    } else {
      $scope.showStatus = 2;
    }

    $scope.getConsultOrderPageList();
  }

  //切换订单状态
  $scope.changeOrderType = function ( event ) {
    $scope.orderType = event.currentTarget.dataset.status;
    $scope.getConsultOrderPageList();
  }

  //切换业务类型
  $scope.changeBusinessType = function ( event ) {
    $scope.businessType = event.currentTarget.dataset.code;
    $scope.businessName = event.currentTarget.innerText;
    $scope.getConsultOrderPageList();
    $scope.showYw = false
  }


  //格式化业务类型code
  $scope.formatBusinessType = function ( code ) {
    return $scope.businessListClassify[code].name;
  }

  $scope.panalShow = function () {
    $scope.showYw = !$scope.showYw
  }

  function getCustomerDetails(item) {
    $scope.itemDetails = item;
    if(item.customerType ==1 ){
      $scope.customerType = "个人"
    }else if(item.customerType ==2){
      $scope.customerType = '企业';
    }
    $('#customerDetails').modal("show");
  }

  $scope.getConsultOrderId = function (item) {
    $scope.orderId = item.orderId;
    $("#submit").modal("show");
  };
  //提交验收
  $scope.sumbitAccept = function (consultOrderId) {
    var newCaseSources={
      'consultOrderId':consultOrderId//订单id
    };

    orderReceivingService.sumbitAccept(newCaseSources).then(function(data){
      if(data.data.code==1){
        $scope.sumbitAcceptList=data.data.data;
        getConsultOrderPageList();
      }
    })
  };
  $scope.findConsultOrderInfo=function () {
    sessionStorage.removeItem('parentUrl');
  }
};
